package copyengine.ui.component.list.old
{
	import copyengine.ui.component.list.old.animation.ICEListCellRenderAnimation;
	import copyengine.ui.component.list.old.cellrender.ICEOldCellRender;
	import copyengine.ui.component.list.old.dataprovider.CEMultipleRowDataProvider;
	import copyengine.ui.component.list.old.dataprovider.ICEListDataProvider;
	
	import flash.geom.Rectangle;

	public class CEMultipleRowListCore extends CEListCore
	{
		private var groupPadding:int;

		private var eachGroupDisplayCount:int;

		public function CEMultipleRowListCore(_displayGroupCount:int,
											  _eachGroupDisplayCount:int,
											  _layoutDirection:String,
											  _eachCellRenderWidth:int,
											  _eachCellRenderHeight:int,
											  _contentPadding:int,
											  _groupPadding:int)
		{
			groupPadding = _groupPadding;
			eachGroupDisplayCount = _eachGroupDisplayCount;
			super(_displayGroupCount, _layoutDirection, _eachCellRenderWidth, _eachCellRenderHeight, _contentPadding);
		}

		/**
		 * @inheritDoc
		 */
		protected override function getCurrentFirstProviderDataGroupIndex():int
		{
			return Math.ceil(cellRenderVector[0].cellIndex / eachGroupDisplayCount);
		}

		/**
		 * @inheritDoc
		 */
		protected override function getDataProviderGroupLength():int
		{
			return (dataProvider as CEMultipleRowDataProvider).getGroupLength();
		}

		/**
		 * @inheritDoc
		 */
		protected override function layoutAllCellRender():void
		{
			var cellRenderPos:int = -getFirstCellRenderGroupOffset();
			for (var i:int = 0; i <= displayGroupCount; i++)
			{
				layoutEachGroupCellRender(cellRenderPos, i);
				cellRenderPos += getCellRenderBoundSize() + groupContentPadding;
			}
		}

		private function layoutEachGroupCellRender(_startPos:int, _cellRenderGroupIndex:int):void
		{
			var eachCellRenderOffsetPos:int = 0;
			for (var i:int = 0; i < eachGroupDisplayCount; i++)
			{
				var cellRender:ICEOldCellRender = cellRenderVector[getCellRenderInternalIndex(_cellRenderGroupIndex, i)];
				if (layoutDirection == LAYOUT_HORIZONTAL)
				{
					cellRender.container.x = _startPos;
					cellRender.container.y = eachCellRenderOffsetPos;
					eachCellRenderOffsetPos += eachCellRenderHeight + groupPadding;
				}
				else // layoutDirection == LAYOUT_VERTICAL
				{
					cellRender.container.y = _startPos;
					cellRender.container.x = eachCellRenderOffsetPos;
					eachCellRenderOffsetPos += eachCellRenderWidth + groupPadding;
				}
			}
		}


		/**
		 * @inheritDoc
		 */
		protected override function refreshCellRenderGroup(_cellRenderGroupIndex:int, _dataProviderGroupIndex:int):void
		{
			for (var i:int = 0; i < eachGroupDisplayCount; i++)
			{
				var cellRender:ICEOldCellRender = cellRenderVector[getCellRenderInternalIndex(_cellRenderGroupIndex, i)];
				cellRender.recycle();
				cellRender.cellIndex = (dataProvider as CEMultipleRowDataProvider).getInternalIndex(_dataProviderGroupIndex, i);
				cellRender.setData((dataProvider as CEMultipleRowDataProvider).getDataByIndex(_dataProviderGroupIndex, i));
				cellRender.drawNow();
			}
		}

		/**
		 * @inheritDoc
		 */
		protected override function swapCellRenderGroup(_groupIndex1:int, _groupIndex2:int):void
		{
			if (_groupIndex1 != _groupIndex2)
			{
				for (var i:int = 0; i < eachGroupDisplayCount; i++)
				{
					var index1:int = getCellRenderInternalIndex(_groupIndex1, i);
					var index2:int = getCellRenderInternalIndex(_groupIndex2, i)
					var cellRender:ICEOldCellRender = cellRenderVector[index1];
					cellRenderVector[index1] = cellRenderVector[index2];
					cellRenderVector[index2] = cellRender;
				}
			}
		}


		protected override function disposeAllCellRender():void
		{
			for each (var cellRender:ICEOldCellRender in cellRenderVector)
			{
				cellRender.cellRenderDispose();
			}
			cellRenderVector = null;
		}

		protected override function disposeDataProvider():void
		{
			dataProvider.dispose();
		}

		protected override function initializeAllCellRender(_cellRenderInstanceClass:Class, _cellRenderAnimationClass:Class):void
		{
			cellRenderVector = new Vector.<ICEOldCellRender>();
			for (var i:int = 0; i <= displayGroupCount; i++)
			{
				for (var j:int = 0; j < eachGroupDisplayCount; j++)
				{
					var cellRender:ICEOldCellRender = new _cellRenderInstanceClass();
					var cellRenderAnimation:ICEListCellRenderAnimation;
					if (_cellRenderAnimationClass != null)
					{
						cellRenderAnimation = new _cellRenderAnimationClass();
					}
					cellRender.initialize(cellRenderAnimation);
					addChild(cellRender.container);
					cellRenderVector.push(cellRender);
				}
			}
		}

		protected override function setDataProvider(_dataProvider:ICEListDataProvider):void
		{
			dataProvider = _dataProvider as CEMultipleRowDataProvider;
			if (dataProvider == null)
			{
				dataProvider = new CEMultipleRowDataProvider();
			}
			(dataProvider as CEMultipleRowDataProvider).setCellLength(eachGroupDisplayCount);
		}

		protected override function initListCoreScrollRect():void
		{
			if (layoutDirection == LAYOUT_HORIZONTAL)
			{
				this.scrollRect = new Rectangle(0, 0, displayGroupCount * (eachCellRenderWidth + groupContentPadding) - groupContentPadding,
												(eachCellRenderHeight + groupPadding) * eachGroupDisplayCount - groupPadding);
			}
			else // layout == LAYOUT_VERTICAL
			{
				this.scrollRect = new Rectangle(0, 0, (eachCellRenderWidth + groupPadding) * eachGroupDisplayCount - groupPadding,
												displayGroupCount * (eachCellRenderHeight + groupContentPadding) - groupContentPadding);
			}
		}

		private function getCellRenderInternalIndex(_cellRenderGroupIndex:int, _cellIndex:int):int
		{
			return _cellRenderGroupIndex * eachGroupDisplayCount + _cellIndex;
		}


	}
}